import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;


public class ResultsView implements IView
{

	public ResultsView(QuizController ctrl, ResultsModel results)
	{
		itsQuizController = ctrl;
		itsResultsModel = results;
	}
	
	@Override
	public void display(JFrame frame)
	{
		JDialog dialog = new JDialog(frame, "Results");
		
		itsResultsTable = new JTable(itsResultsModel);
		itsResultsTable.getColumn("Click to view").setCellRenderer(new ButtonRenderer());
		itsResultsTable.getColumn("Click to view").setCellEditor(new ButtonEditor(new JCheckBox(), itsQuizController, dialog));
		    
		JScrollPane scrollPane = new JScrollPane(itsResultsTable);
		
		JPanel resultsPanel = new JPanel();
		resultsPanel.setLayout(new BoxLayout(resultsPanel, BoxLayout.PAGE_AXIS));
		resultsPanel.add(scrollPane);
		
		
		dialog.getContentPane().setLayout(new BorderLayout());
		dialog.getContentPane().add(resultsPanel,  BorderLayout.CENTER);
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - dialog.getWidth()) / 2;
		final int y = (screenSize.height - dialog.getHeight()) / 2;
		dialog.setLocation(x, y);
		
		dialog.pack();
		dialog.setVisible(true);
	}

	
	private QuizController itsQuizController;
	private ResultsModel itsResultsModel;
	private JTable itsResultsTable;
}


// code from: http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm

class ButtonRenderer extends JButton implements TableCellRenderer 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ButtonRenderer() 
	{
		setOpaque(true);
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
	{
		if(isSelected) 
		{
			setForeground(table.getSelectionForeground());
			setBackground(table.getSelectionBackground());
		}
		else 
		{
			setForeground(table.getForeground());
			setBackground(UIManager.getColor("Button.background"));
		}
		setText((value == null) ? "" : value.toString());
		return this;
	}
}

class ButtonEditor extends DefaultCellEditor
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected JButton button;
	private String label;
	private boolean isPushed;
	private QuizController itsQuizController;
	private JDialog parent;
	private int clickedRow;

	public ButtonEditor(JCheckBox checkBox, QuizController ctrl, JDialog parent) 
	{
		super(checkBox);
		itsQuizController = ctrl;
		this.parent = parent;
		button = new JButton();
		button.setOpaque(true);
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				fireEditingStopped();
			}
		});
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
	{
		if(isSelected)
		{
			button.setForeground(table.getSelectionForeground());
			button.setBackground(table.getSelectionBackground());
		}
		else
		{
			button.setForeground(table.getForeground());
			button.setBackground(table.getBackground());
		}
		label = (value == null) ? "" : value.toString();
		button.setText(label);
		isPushed = true;
		clickedRow = row;
		return button;
	}

	public Object getCellEditorValue()
	{
		if(isPushed)
		{
			//JOptionPane.showMessageDialog(button, label + ": Ouch!");
			itsQuizController.showAnsweredQuestion(clickedRow, parent);
		}
		isPushed = false;
		return new String(label);
	}

	public boolean stopCellEditing()
	{
		isPushed = false;
		return super.stopCellEditing();
	}

	protected void fireEditingStopped()
	{
		super.fireEditingStopped();
	}
}
